/**
 * 
 */
package org.longteng.monitor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Map;

/**
 * @author hymn
 *
 */
@Slf4j
@Component
public class SpringWebConfig extends SpringBootServletInitializer implements ApplicationContextAware, ApplicationRunner, WebMvcConfigurer {
	private ApplicationContext applicationContext;

	RequestMappingHandlerMapping handlerMapping;

	@Override
	protected SpringApplicationBuilder configure(
			SpringApplicationBuilder application) {
		return application.sources(SpringStartApp.class);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {
		handlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
		if (handlerMapping == null) {
			log.warn("handlerMapping为null,无法监控系统发布的api。");
		} else {
			Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
			log.info("系统发布的api：{}", handlerMethods);

		}
	}

}
